package com.rgsx.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rgsx.dao.OrderDao;
import com.rgsx.entiy.Position;
import com.rgsx.mapper.OrderMapper;
import com.rgsx.mapper.UserMapper;
import com.rgsx.pojo.Order;
import com.rgsx.pojo.User;
import com.rgsx.service.OrderService;
import com.rgsx.utils.JWTUtil;
import com.rgsx.utils.RedisPositionUtil;
import com.rgsx.vo.ResultVo;
import com.rgsx.vo.entiyVo.OrderVo;
import com.rgsx.vo.entiyVo.PosListItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;


@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderDao orderDao;

    @Resource
    private RedisTemplate redisTemplate;

    //发起订单（客户）
    @Override
    public ResultVo OrderOrigination(Position positionStart, Position positionEnd, String token) {
        long passengerId = JWTUtil.getUserId(token);

        Order order = orderDao.checkIsHaveOrderNotDone(passengerId);
        if(order != null){
            return ResultVo.fail("已经有存在的订单信息","2");
        }

        Long orderId = orderDao.OrderOrigination(passengerId, positionStart, positionEnd);

        HashMap<String, String> result = new HashMap<>();
        result.put("orderId", String.valueOf(orderId));

        if(orderId != null)return ResultVo.success("成功",result);
        return ResultVo.fail("失败","2");
    }

    //司机接受订单
    @Override
    public ResultVo ReceivingOrders(long orderId,long driverId) {
        Order order = orderDao.ReceivingOrders(orderId, driverId);
        if(order == null){
            return ResultVo.fail("接收订单失败,联系后端查看日志","2");
        }
        return ResultVo.success("成功",new OrderVo(order));
    }

    //上车(司机端)
    @Override
    public ResultVo GetOnTheCar( long orderId ,String userName) {
        User user = orderMapper.selectUserByOrderIdAndUserName(orderId, userName);

        if(user == null){
            return ResultVo.fail("手机号错误","2");
        }
        Order order = orderDao.GetOnTheCar(orderId);

        return ResultVo.success("成功",new OrderVo(order));
    }

    //订单结束
    @Override
    public ResultVo orderOver(long orderId,Position position) {
        boolean isSuccess = orderDao.orderOver(orderId);
        if(isSuccess)return ResultVo.success("结束成功",null);
        return ResultVo.fail("结束失败，联系后端查看日志","2");
    }

    //拿到进行中订单(乘客)
    @Override
    public ResultVo getAllOrder(long passagerId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("passenger_id",passagerId);
        queryWrapper.in("state",0,1,2);

        Order order = orderMapper.selectOne(queryWrapper);

        if(order != null){
            redisTemplate.opsForValue().set("order:" + order.getId(),order);
            //还没被接单
            if(order.getState() == 0){
                //刷新redis里面的GEO类型
                RedisPositionUtil.setGeoToRedis(redisTemplate,
                        order.getId(),
                        new Position(order.getPositionStartLatitude(),
                                order.getPositionStartLongitude()));
            }
            return ResultVo.success("成功",new OrderVo(order));
        }

        return ResultVo.fail("没有订单信息","2");
    }

    //获取某司机附近的乘客（司机端）
    @Override
    public ResultVo getNearbyOrder(long driverId,Position position) {
        List<PosListItem> posListItems = RedisPositionUtil.nearBySearch(100,
                Double.valueOf(position.getPositionLongitude()),
                Double.valueOf(position.getPositionLatitude()),
                redisTemplate);

        return ResultVo.success("获取完毕",posListItems);
    }

    //司机获取自己接的单
    @Override
    public ResultVo getDriverOrder(long driverId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("driver_id",driverId);
        queryWrapper.in("state",0,1,2);

        Order order = orderMapper.selectOne(queryWrapper);
        if(order != null){
            redisTemplate.opsForValue().set("order:" + order.getId(),order);
        }


        return ResultVo.success("成功",new OrderVo(order));
    }
}
